package ar.edu.utn.frsf.tpae.a2014.g02.persistence.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.edu.utn.frsf.tpae.a2014.g02.persistence.dao.EmpleadoDao;
import ar.edu.utn.frsf.tpae.a2014.g02.persistence.model.Empleado;

/**
 * DAO de Empleado basado en Hibernate.
 * 
 * @author TPAE
 */
@Component
public class EmpleadoHibernateDao implements EmpleadoDao {

	  private SessionFactory sessionFactory;

	  // Setter utilizado por Spring.
	  @Autowired(required = true)
	  public void setSessionFactory(SessionFactory factory) {
	    this.sessionFactory = factory;
	  }

	  private Session getCurrentSession() {
	    return sessionFactory.getCurrentSession();
	  }

	public Empleado persistir(Empleado usuario) {
		getCurrentSession().saveOrUpdate(usuario);
		return usuario;
	}

	@SuppressWarnings("unchecked")
	
	public List<Empleado> listar() {
		Query query = getCurrentSession().createQuery("from " + Empleado.class.getName());
		return query.list();
	}

	
	public void deleteAll() {
		Query query = getCurrentSession().createQuery("delete from " + Empleado.class.getName());
		query.executeUpdate();
	}
}
